home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / IFF / old2ilbm.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  8KB  |  255 lines

  1. /***************************************************************************
  2. *  Old2ILBM.c --  Convert old LoRes 5-plane .pic file to ILBM file
  3. *                 by Carolyn Scheppner  CBM  01/86
  4. *     Using IFF rtns by J.Morrison and S.Shaw of Electronic Arts
  5. *
  6. *     Notes: 1. Headers changed to allow compilation under 1.0
  7. *            2. This program does not create a .info file for
  8. *               the new IFF file. If need an icon for the file,
  9. *               (ex. for Graphicraft), copy the .info file
  10. *               for another picture and rename it to your
  11. *               IFF file name with a .info suffix.
  12. *
  13. * Alink information:
  14. * FROM     B:LStartup.obj,SRC:Old2ILBM.o,SRC:iffw.o,SRC:ilbmw.o,SRC:packer.o
  15. * TO       SRC:Old2ILBM
  16. * LIBRARY  B:lc.lib,B:amiga.lib
  17. *
  18. *  Note: B: assigned to libs/  and SRC: assigned to dir of .o files
  19. *
  20. ***************************************************************************/
  21.  
  22. #include <exec/types.h>
  23. #include <exec/exec.h>
  24.  
  25. #include <graphics/gfx.h>
  26. #include <graphics/gfxbase.h>
  27. #include <graphics/copper.h>
  28. #include <graphics/gels.h>
  29. #include <graphics/regions.h>
  30.  
  31. #include <hardware/blit.h>
  32.  
  33. #include <devices/keymap.h>
  34.  
  35. #include <intuition/intuition.h>
  36. #include <intuition/intuitionbase.h>
  37.  
  38. #include <libraries/dos.h>
  39. #include <iff/ilbm.h>
  40.  
  41. #define bufSize 512
  42. #define DEPTH    5
  43. #define WIDTH  320
  44. #define HEIGHT 200
  45.  
  46. struct IntuitionBase *IntuitionBase;
  47. struct GfxBase       *GfxBase;
  48. ULONG  DosBase;
  49.  
  50. struct BitMap   oldBitMap;
  51. struct OldHeader {
  52.           WORD  filetype;      /* 0000 = pic */
  53.           WORD  compression;
  54.           WORD  bitplanes;     /* usually stored as 0000 */
  55.           WORD  mini;
  56.           ULONG datasize;      /* 0000FA00 for 320x200, 5 planes */
  57.           WORD  xorig;
  58.           WORD  yorig;
  59.           WORD  height;
  60.           WORD  width;         /* in bytes ! */
  61.           } oldHeader = {0};
  62. struct OldColor {              /* Old file format */
  63.           BYTE  red;
  64.           BYTE  green;
  65.           BYTE  blue;
  66.           BYTE  pad;
  67.           } oldColor = {0};
  68. WORD   colorTable[32];      /* Amiga color table format */
  69.  
  70. int i;
  71.  
  72. /* main() */
  73. void main(argc,argv)
  74. int  argc;
  75. char **argv;
  76.    {
  77.    LONG oldfile;
  78.    LONG newfile;
  79.    IFFP iffp = NO_FILE;
  80.    WORD colortemp;
  81.    int  planebytes;
  82.  
  83.    printf("\nOLd2ILBM - Converts old IG 320x200 5-plane .pic to IFF\n\n");
  84.  
  85.    if (argc < 3)
  86.       printf("   Usage: 'Old2ILBM oldfilename newfilename'\n\n");
  87.    else
  88.       {
  89.       if ((IntuitionBase =
  90.        (struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL)
  91.          cleanexit(-1);
  92.  
  93.       if ((GfxBase =
  94.        (struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
  95.          cleanexit(-1);
  96.  
  97.       if ((DosBase = OpenLibrary("dos.library",0))==NULL)
  98.          cleanexit(-1);
  99.  
  100.       if ((oldfile = Open(argv[1], MODE_OLDFILE))==NULL)
  101.          printf("Can't open %s\n\n",argv[1]);
  102.       else
  103.          {
  104.          if ((newfile = Open(argv[2], MODE_NEWFILE))==NULL)
  105.             printf("Can't open %s\n\n",argv[2]);
  106.          else
  107.             {
  108.             InitBitMap(&oldBitMap,DEPTH,WIDTH,HEIGHT);
  109.             for(i=0; i < DEPTH; i++)
  110.                {
  111.                if((oldBitMap.Planes[i] =
  112.                 (PLANEPTR)AllocRaster(WIDTH,HEIGHT)) == NULL)
  113.                   {
  114.                   printf("Can't allocate RAM for bitmap.\n");
  115.                   cleanexit(-1); 
  116.                   }
  117.                }
  118.  
  119.             printf(" Note: This program does not create a .info file\n");
  120.             printf("       for the IFF picture file.  If you need an\n");
  121.             printf("       icon, copy a picture .info file from your\n");
  122.             printf("       IFF paint package and give it the same name\n");
  123.             printf("       as your picture file adding a .info suffix.\n\n");
  124.  
  125.             printf("\nConverting '%s' to IFF '%s'...\n\n", argv[1],argv[2]);
  126.  
  127.             Read(oldfile,(BYTE *)&oldHeader,20);
  128.             if((oldHeader.width != 40)||(oldHeader.height != 200))
  129.                {
  130.                printf("Warning: Old pic x/y sizes are hex %x by %x.\n",
  131.                         oldHeader.width,oldHeader.height);
  132.                }
  133.             for (i=0; i < (1<<DEPTH); i++)
  134.                {
  135.                 colortemp = 0;
  136.                 Read(oldfile,(BYTE *)&oldColor,4);
  137.                 colortemp = oldColor.red;
  138.                 colortemp = colortemp << 4;
  139.                 colortemp |= oldColor.green;
  140.                 colortemp = colortemp << 4;
  141.                 colortemp |= oldColor.blue;
  142.                 colorTable[i] = colortemp;
  143.                 }
  144.  
  145.             planebytes = (int)((WIDTH/8)*HEIGHT);
  146.             for(i=0; i < DEPTH; i++)
  147.                {
  148.                Read(oldfile,oldBitMap.Planes[i],planebytes);
  149.                }
  150.  
  151.             Write(newfile,"x",1);  /* so Seek to beginning works ? */
  152.  
  153.             iffp = PutPicture(newfile, &oldBitMap, &colorTable[0]);
  154.  
  155.             Close(newfile);
  156.             }
  157.          Close(oldfile);
  158.          }
  159.  
  160.       cleanup();
  161.       }
  162.    }
  163.  
  164.  
  165. cleanexit(error)
  166. int error;
  167.    {
  168.    cleanup();
  169.    exit(error);
  170.    }
  171.  
  172. cleanup()
  173.    {
  174.    for (i=0; i < DEPTH; i++)
  175.       {
  176.       if(oldBitMap.Planes[i] != NULL)
  177.        FreeRaster(oldBitMap.Planes[i],WIDTH,HEIGHT);
  178.       }
  179.  
  180.    if (DosBase) CloseLibrary(DosBase);
  181.    if (GfxBase) CloseLibrary(GfxBase);
  182.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  183.    }
  184.  
  185. /** PutPicture() ***********************************************************
  186.  *
  187.  * Put a picture into an IFF file.
  188.  * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
  189.  * a NULL mask, and a locally-allocated buffer. It also assumes you want to
  190.  * write out all the bitplanes in the BitMap.
  191.  *
  192.  ***************************************************************************/
  193. Point2D nullPoint = {0, 0};
  194.  
  195. IFFP PutPicture(file, bitmap, colorMap)
  196.       LONG file;  struct BitMap *bitmap;  WORD *colorMap;
  197.    {
  198.    BYTE buffer[bufSize];
  199.    return( PutAnILBM(file, bitmap, NULL,
  200.            colorMap, bitmap->Depth, &nullPoint,
  201.            buffer, bufSize) );
  202.    }    
  203.  
  204.    
  205. /** PutAnILBM() ************************************************************
  206.  *
  207.  * Write an entire BitMap as a FORM ILBM in an IFF file.
  208.  * This version works for any display mode (C. Scheppner).
  209.  *
  210.  * Normal return result is IFF_OKAY.
  211.  *
  212.  * The utility program IFFCheck would print the following outline of the
  213.  * resulting file:
  214.  *
  215.  *   FORM ILBM
  216.  *     BMHD
  217.  *     CMAP
  218.  *     BODY       (compressed)
  219.  *
  220.  ***************************************************************************/
  221. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  222.  
  223. IFFP PutAnILBM(file, bitmap, mask, colorMap, depth, xy, buffer, bufsize)
  224.       LONG file;  struct BitMap *bitmap;  BYTE *mask;  WORD *colorMap;
  225.       UBYTE depth;  Point2D *xy;
  226.       BYTE *buffer;  LONG bufsize;
  227.    {
  228.    BitMapHeader bmHdr;
  229.    GroupContext fileContext, formContext;
  230.    IFFP ifferr;
  231.    WORD pageWidth, pageHeight;
  232.  
  233.    pageWidth  = (bitmap->BytesPerRow) << 3;
  234.    pageHeight = bitmap->Rows;
  235.  
  236.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  237.                       cmpByteRun1, 0, pageWidth, pageHeight);
  238.    /* You could write an uncompressed image by passing cmpNone instead
  239.     * of cmpByteRun1 to InitBMHdr. */
  240.    bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
  241.    if (mask != NULL) bmHdr.masking = mskHasMask;
  242.    bmHdr.x = xy->x;   bmHdr.y = xy->y;
  243.  
  244.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  245.    CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  246.  
  247.    CkErr( PutBMHD(&formContext, &bmHdr) );
  248.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  249.    CkErr( PutBODY(&formContext, bitmap, mask, &bmHdr, buffer, bufsize) );
  250.  
  251.    CkErr( EndWGroup(&formContext) );
  252.    CkErr( CloseWGroup(&fileContext) );
  253.    return( ifferr );
  254.    }    
  255.